import os
from telnetlib import EC

from loguru import logger
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By


from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from commonDemo.common_element import BaseAction


class pdmPage(BaseAction):
    # 定位元素 第一个主泵
    talentText = By.XPATH, "//*[@id='root']/div/div/div[1]/div/div[1]/div[2]/div/div[1]/div/div[1]/div[2]/div[1]"
    tabs = By.XPATH, "//*[@id='root']/section/header/div[2]"   #标题
    assert_text = By.XPATH, "//*[@id='monitorGplot']"   #图片元素 用作断言
    pdm_text = By.XPATH, "//div[text()='PdM平台']"   # 平台名称
    task_text = By.XPATH, "//span[text()='任务管理']"   # 任务管理
    history_text = By.XPATH,"//div[@class='ant-tabs-tab-btn' and text()='历史任务']"   # 历史任务
    infrared_text = By.XPATH, "//span[text()='红外诊断']"   # 红外诊断
    add_test=By.XPATH,"//span[text()='新建']"   #新建
    add01=By.XPATH,"//div[text()='振动分析' and @class='tag___KBvJw']"   #新增-振动分析
    add02=By.XPATH,"//div[text()='红外诊断' and @class='tag___KBvJw']"   #新增-红外诊断
    add03=By.XPATH,"//div[text()='超声检测' and @class='tag___KBvJw']"   #新增-超声检测
    add04=By.XPATH,"//div[text()='声学成像' and @class='tag___KBvJw']"   #新增-声学成像
    add05=By.XPATH,"//div[text()='润滑分析' and @class='tag___KBvJw']"   #新增-润滑分析
    add06=By.XPATH,"//div[text()='电机诊断' and @class='tag___KBvJw']"   #新增-电机诊断
    add07=By.XPATH,"//div[text()='整体评估' and @class='tag___KBvJw']"   #新增-整体分析
    task01=By.XPATH,"//span[@class='tag' and text()='振动分析']"   #振动分析
    task02=By.XPATH,"//span[@class='tag' and text()='红外诊断']"   #红外诊断
    task03=By.XPATH,"//span[@class='tag' and text()='超声检测']"   #超声检测
    task04=By.XPATH,"//span[@class='tag' and text()='声学成像']"   #声学成像
    task05=By.XPATH,"//span[@class='tag' and text()='润滑分析']"   #润滑分析
    task06=By.XPATH,"//span[@class='tag' and text()='电机诊断']"   #电机诊断
    task07=By.XPATH,"//span[@class='tag' and text()='整体评估']"   #整体评估
    # 设备筛选
    device01=By.XPATH,"(//span[@class='ant-tree-switcher ant-tree-switcher_close'])[1]"
    device01_01 = By.XPATH, "(//span[@class='ant-tree-switcher ant-tree-switcher_close'])[6]"
    device01_02=By.XPATH,"(//span[@class='ant-tree-checkbox-inner'])[1]"
    device01_03 = By.XPATH, "(//span[@class='ant-tree-switcher ant-tree-switcher_close'])[4]"
    confirm=By.XPATH,"// button / span[text() = '确 认']"
    confirm01=By.XPATH,"// button / span[text() = '确 定']"
    submit=By.XPATH,"// button / span[text() = '提 交']"
    submit_2=By.XPATH,"(// button / span[text() = '提 交'])[2]"    #二次确认
    detaile_text=By.XPATH,"// button / span[text() = '详 情']"
    delete = By.XPATH, "// button / span[text() = '删 除']"

    input_WOT=By.ID,"wot"
    addModel=By.XPATH,"//button/span[text()='创建任务']"
    infraredtext=By.XPATH,"//span[text()='红外台账']"   #红外台账
    select_text=By.XPATH,"//th[@class='ant-table-cell ant-table-selection-column']"   #全选
    derive_text=By.XPATH,"//button[@class='ant-btn ant-btn-primary' ]"   #导出
    roll_text1=By.XPATH,"(//div[@class='three_row_ellipsis'])[20]"   #滚动条
    roll_text2=By.XPATH,"//span[@class='link_text___1yPXv' and text()='IT2016050013']"   #红外诊断-红外台账-滚动条
    infraredback=By.XPATH,"//button[@class='ant-btn back___2s8PB']"   #返回
    search_text=By.XPATH,"//input[@placeholder='任务编号/设备编码/设备名称/WOT' and @class='ant-input' and @type='text']"   #搜索框
    search_button=By.XPATH,"//button/span[text()='搜 索']"   #搜索按钮
    handle_text=By.XPATH,"(//button[@class='ant-btn ant-btn-primary ant-btn-sm ant-btn-background-ghost' and span[text()='去处理']])[1]"   #处理
    confirm_evaluate=By.XPATH,"//span[text()='确认评估']"   #确认评估
    confirm_only=By.XPATH,"//span[text()='仅确认']"   #仅确认
    mission_text=By.XPATH,"(//span[@class='ant-descriptions-item-content'])[1]"   #任务编号
    device_text=By.XPATH,"(//span[@class='ant-descriptions-item-content'])[3]"   #设备编码
    measure_text=By.XPATH,"(//td[@class='ant-table-cell' and @style='text-align: center;'])[4]"   #测量数值/超声波强度
    measure_text01=By.ID,"monitorvalue"   #测量数值
    UL_strength=By.ID,"strength"   #超声波强度
    sensibility=By.ID,"sensibility"   #灵敏度
    frequency=By.ID,"frequency"   #测量频率（KHz）
    remarks_text=By.XPATH,"//div/textarea[@class='ant-input']"   #备注
    evaluate_text=By.XPATH,"//span[@class='ant-select-selection-search']"   #评估
    wear_index=By.XPATH,"(//span[@class='ant-select-selection-search'])[1]"           #润滑分析技术评估-磨损指标  创建任务-设备编码   振动分析-创建任务-检测设备
    pollution_index=By.XPATH,"(//span[@class='ant-select-selection-search'])[2]"   #润滑分析技术评估-污染指标    创建任务-触发来源    振动分析-创建任务-检测探头
    triggersource=By.XPATH,"(//span[@class='ant-select-selection-item'])[2]"   #触发来源
    task_text1=By.XPATH,"//div[text()='任务信息']"   #任务信息
    aging_index=By.XPATH,"(//span[@class='ant-select-selection-search'])[3]"  #润滑分析技术评估-老化指标     创建任务-任务负责人
    evaluate_index=By.XPATH,"(//span[@class='ant-select-selection-search'])[4]"      #润滑分析技术评估-本次评估
    input_data=By.XPATH,"(//span[@class='ant-btn ant-btn-dashed'])"   #润滑分析-数据导入按钮

    evaluate_value=By.XPATH,"//input[@aria-activedescendant='rc_select_0_list_0']"   #可接受
    evaluate_advice0=By.XPATH,"(//div[@class='ant-form-item-control-input-content']/textarea)[1]"   #整体评估-评估意见
    evaluate_advice=By.XPATH,"(//div[@class='ant-form-item-control-input-content']/textarea)[2]"   #评估意见  整体评估-行动项
    handle_advice=By.XPATH,"(//div[@class='ant-form-item-control-input-content']/textarea)[3]"   #处理建议
    handle_button=By.XPATH,"//label[text()='处理建议']"   #处理建议文本
    save_text=By.XPATH,"//button/span[text()='保 存']"   #保存
    create_time=By.XPATH,"(//span[@class='global_text_btn___mE8xw fc_primary___3kmyS iconfont icon-bianjisekuai'])[1]"   #创建时间
    select_time=By.XPATH,"(//input[@placeholder='请选择日期'])[1]"   #选择时间
    year_text=By.XPATH,"//button[@class='ant-picker-year-btn']"   #年
    month_text=By.XPATH,"//button[@class='ant-picker-month-btn']"   #月
    year_2025=By.XPATH,"//div[@class='ant-picker-cell-inner' and text()='2025']"   #2025
    month_1=By.XPATH,"//div[@class='ant-picker-cell-inner' and text()='1月']"   #1月
    date_1=By.XPATH,"(//div[@class='ant-picker-cell-inner' and text()='1'])[1]"   #1号
    wot_text=By.XPATH,"//span[@class='global_text_btn___mE8xw fc_primary___3kmyS iconfont icon-bianjisekuai']"  #WOT
    wot_edit=By.ID,"editWot"   #WOT编辑框
    time_text=By.XPATH,"//div[@class='ant-picker-input']"   #数据时间
    now_time=By.XPATH,"//a[@class='ant-picker-now-btn' and text()='此刻']"   #当前时间
    upload_button=By.XPATH,"(//div[text()='请上传红外数据'])[1]"   #上传红外数据
    upload_button2=By.XPATH,"(//div[text()='请上传红外数据'])[2]"   #上传红外数据
    upload_button1=By.XPATH,"//div[text()='请上传超声数据']"   #上传超声数据
    buttton_file=By.XPATH,"//button[@class='ant-btn ant-btn-default']"   #上传附件
    alpha_text=By.XPATH,"//span[text()='阿尔法']"   #阿尔法
    environment_temperature=By.ID,"airtemperature" #环境温度
    reflect_temperature=By.ID,"reflecttemperature" #反射温度
    relative_humidity=By.ID,"airwet"  #相对湿度
    distance=By.ID,"distance"   #距离
    radiation=By.ID,"radiation"   #辐射率
    assert_text1=By.XPATH,"//span[text()='创建成功']"   #创建成功
    assert_text1_1=By.XPATH,"//span[contains(text(),'该任务未结束')]"   #任务未结束，创建失败
    assert_text2=By.XPATH,"//span[text()='提交成功']"   #提交成功

    assert_text3=By.XPATH,"//span[text()='删除成功']"   #删除成功
    task_encoding=By.XPATH,"//div[@label='1-CGS-MP-001A']" #任务编码
    person_text=By.XPATH,"//div[@label='郑凯（20221873）']"   #任务负责人

    files_input=By.XPATH, "//input[@type='file']"   #文件上传输入框
    files_input2=By.XPATH, "(//input[@type='file'])[2]"

    def talentClick(self):
        self.click(self.talentText)
        logger.info("点击主泵")

    def tabsClick(self):
        self.click(self.tabs)

    def assert_img(self):
        self.click(self.assert_text)
        logger.info("断言点击图片")

    def click_tab(self):
        self.click(self.tabs)
        logger.info("点击标题返回首页")

    def pdm(self):
        self.click(self.pdm_text)
        logger.info("进入pdm平台")

    def task(self):
        self.click(self.task_text)
        logger.info("进入任务管理")

    def history(self):
        self.click(self.history_text)
        logger.info("进入历史任务")

    def infrared(self):
        self.click(self.infrared_text)
        logger.info("进入红外诊断")

    def add_test_click(self):
        self.click(self.add_test)
        logger.info("点击新建")

    def add01_click(self):
        self.click(self.add01)
        logger.info("点击新增-振动分析")

    def add02_click(self):
        self.click(self.add02)
        logger.info("点击新增-红外诊断")

    def add03_click(self):
        self.click(self.add03)
        logger.info("点击新增-超声检测")

    def add04_click(self):
        self.click(self.add04)
        logger.info("点击新增-声学成像")

    def add05_click(self):
        self.click(self.add05)
        logger.info("点击新增-润滑分析")

    def add06_click(self):
        self.click(self.add06)
        logger.info("点击新增-电机诊断")

    def add07_click(self):
        self.click(self.add07)
        logger.info("点击新增-整体分析")

    def click_task01(self):
        self.click(self.task01)
        logger.info("点击振动分析")

    def click_task02(self):
        self.click(self.task02)
        logger.info("点击红外诊断")

    def click_task03(self):
        self.click(self.task03)
        logger.info("点击超声检测")

    def click_task04(self):
        self.click(self.task04)
        logger.info("点击声学成像")

    def click_task05(self):
        self.click(self.task05)
        logger.info("点击润滑分析")

    def click_task06(self):
        self.click(self.task06)
        logger.info("点击电机诊断")

    def click_task07(self):
        self.click(self.task07)
        logger.info("点击整体评估")
    def device01_click(self):
        self.click(self.device01)
        logger.info("点击第一个设备")

    def device01_01_click(self):
        self.click(self.device01_01)
        logger.info("点击设备号")
    def device01_03_click(self):
        self.click(self.device01_03)
        logger.info("点击设备号")

    def device01_02_click(self):
        self.click(self.device01_02)
        logger.info("勾选设备")



    def confirm_click(self):
        self.click(self.confirm)
        logger.info("点击确认")

    def confirm01_click(self):
        self.click(self.confirm01)
        logger.info("点击确定")

    def click_submit(self):
        self.click(self.submit)
        logger.info("点击提交")

    def click_submit_2(self):
        self.click(self.submit_2)
        logger.info("点击二次确认提交")

    def detaile_click(self):
        self.click(self.detaile_text)
        logger.info("点击详情")

    def delete_click(self):
        self.click(self.delete)
        logger.info("点击删除")

    def input_WOT_value(self,value):
        self.input(self.input_WOT,value)
        logger.info("输入WOT值")

    def addModel_click(self):
        self.click(self.addModel)
        logger.info("点击创建任务")

    def infraredtext_click(self):
        self.click(self.infraredtext)
        logger.info("点击红外台账")

    def infraredtext_roll(self):
        self.execute_script_1(1000)
        logger.info("红外台账滚动")

    def select_all(self):
        self.click(self.select_text)
        logger.info("全选")

    def derive_click(self):
        self.click(self.derive_text)
        logger.info("导出")

    def roll_down(self):
        self.execute_script_2(self.evaluate_text)
        logger.info("滚动条下拉")

    # def roll_down1(self):
    #     self.execute_script_2(self.roll_text2)
    #     logger.info("红外诊断-红外台账-滚动条下拉")


    def infraredback_click(self):
        self.click(self.infraredback)
        logger.info("返回")

    def input_search_value(self,value):
        self.input(self.search_text,value)
        logger.info("查找值")

    def search_click(self):
        self.click(self.search_button)
        logger.info("点击搜索按钮")

    def handle_click(self):
        self.click(self.handle_text)
        logger.info("点击去处理")

    def click_confirm_evaluate(self):
        self.click(self.confirm_evaluate)
        logger.info("点击确认评估")

    def click_confirm_only(self):
        self.click(self.confirm_only)
        logger.info("点击仅确认")

    def get_mission_text(self):
        text=self.get_text(self.mission_text)
        logger.info("获取任务编号")
        return text

    def get_device_text(self):
        text=self.get_text(self.device_text)
        logger.info("获取设备编码")
        return text

    def get_measure_text(self):
        text=self.get_text(self.measure_text)
        logger.info("获取测量数值")
        return text

    def remarks_input(self,value):
        self.input(self.remarks_text,value)
        logger.info("输入备注:test")

    def evaluate_click(self):
        self.click(self.evaluate_text)
        logger.info("点击本次评估")

    def evaluate_value_click(self):
        self.click(self.evaluate_value)
        logger.info("选择可接受")

    def input_evaluate_advice(self,value):
        self.input(self.evaluate_advice,value)
        logger.info("输入评估意见")

    def input_evaluate_advice0(self,value):
        self.input(self.evaluate_advice0,value)
        logger.info("输入评估意见")

    def input_handle_advice(self,value):
        self.input(self.handle_advice,value)
        logger.info("输入处理建议")

    def save_click(self):
        self.click(self.save_text)
        logger.info("点击保存")

    def click_create_time(self):
        self.click(self.create_time)
        logger.info("点击创建时间")

    def click_create_time_select(self):
        self.click(self.select_time)
        logger.info("点击选择时间")

    def click_create_time_year(self):
        self.click(self.year_text)
        logger.info("点击年")

    def click_year_2025(self):
        self.click(self.year_2025)
        logger.info("选择2025年")

    def click_create_time_month(self):
        self.click(self.month_text)
        logger.info("点击月")

    def click_month_1(self):
        self.click(self.month_1)
        logger.info("选择1月")

    def click_date_1(self):
        self.click(self.date_1)
        logger.info("选择1号")

    def click_wot(self):
        self.click(self.wot_text)
        logger.info("点击WOT")

    def input_wot(self,value):
        self.input(self.wot_edit,value)
        logger.info("输入WOT")

    def click_time(self):
        self.click(self.time_text)
        logger.info("点击数据时间")

    def click_now_time(self):
        self.click(self.now_time)
        logger.info("选择此刻")

    def input_measured_value(self,value):
        self.input(self.measure_text01,value)
        logger.info("输入测量数值")

    def input_UL_strength(self,value):
        self.input(self.UL_strength,value)
        logger.info("输入超声波强度")

    def input_sensibility(self,value):
        self.input(self.sensibility,value)
        logger.info("输入灵敏度")

    def input_frequency(self,value):
        self.input(self.frequency,value)
        logger.info("输入测量频率")

    def roll_down1(self):
        self.execute_script_2(self.handle_button)
        logger.info("滚动条下拉")

    def click_upload_button(self):
        self.click(self.upload_button)
        logger.info("点击上传红外数据")

    def click_upload_button2(self):
        self.click(self.upload_button2)
        logger.info("点击上传红外数据")

    def click_upload_button1(self):
        self.click(self.upload_button1)
        logger.info("点击上传超声数据")

    def click_alpha(self):
        self.click(self.alpha_text)
        logger.info("点击阿尔法")



    def input_env_tmp(self,value):
        self.input(self.environment_temperature,value)
        logger.info("输入环境温度")
    def input_refected_tmp(self,value):
        self.input(self.reflect_temperature,value)
        logger.info("输入反射温度")
    def input_relative_humidity(self,value):
        self.input(self.relative_humidity,value)
        logger.info("输入相对湿度")
    def input_distance(self,value):
        self.input(self.distance,value)
        logger.info("输入距离")
    def input_radiation(self,value):
        self.input(self.radiation,value)
        logger.info("输入辐射率")

    def select_option(self, target_text):
        """
        等待下拉选项出现并选择指定文本的选项
        """
        wait = WebDriverWait(self.driver, 5)
        # 等待下拉选项出现
        option_elements = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '.ant-select-item')))
        for option in option_elements:
            if option.text == target_text:
                # 找到目标选项，点击并返回
                option.click()
                break



    def click_wear_index(self):
        self.click(self.wear_index)
        logger.info("点击磨损指标")

    def click_pollution_index(self):
        self.click(self.pollution_index)
        logger.info("点击污染指标")


    def click_aging_index(self):
        self.click(self.aging_index)
        logger.info("点击老化指标")

    def click_evaluate_index(self):
        self.click(self.evaluate_index)
        logger.info("点击本次评估")

    def get_assert_text1(self):
        text = self.get_text(self.assert_text1)
        logger.info("断言获取提示消息：创建成功")
        return text
    def get_assert_text1_1(self):
        text = self.get_text(self.assert_text1_1)
        logger.info("断言获取提示消息：创建失败")
        return text
    def get_assert_text2(self):
        text = self.get_text(self.assert_text2)
        logger.info("断言获取提示消息：提交成功")
        return text


    def get_assert_text3(self):
        text = self.get_text(self.assert_text3)
        logger.info("断言获取提示消息：删除成功")
        return text

    def input_encoding(self,value):
        self.input(self.wear_index,value)
        logger.info("输入设备编码")
    def input_person(self,value):
        self.input(self.person_text,value)
        logger.info("输入任务负责人")

    def click_task(self):
        self.click(self.task_text1)
        logger.info("点击任务信息")

    def click_triggersource(self):
        self.click(self.triggersource)
        logger.info("点击触发来源")

    def click_data(self):
        self.click(self.input_data)
        logger.info("点击数据导入")

    #导入文件
    def import_file(self, file_name):
        # 定位文件上传输入框
        file_input = self.find_element(self.files_input)
        # 上传文件
        current_dir = os.path.dirname(os.path.abspath(__file__))
        file_path = os.path.join(current_dir, "..", "files", file_name)
        file_input.send_keys(file_path)
    #导入文件2
    def import_file2(self, file_name):
        file_input = self.find_element(self.files_input2)
        current_dir = os.path.dirname(os.path.abspath(__file__))
        file_path = os.path.join(current_dir, "..", "files", file_name)
        file_input.send_keys(file_path)